﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionEngine
{
    /// <summary>
    /// 表达式引擎
    /// </summary>.                                                               
    public static class ExpEngine
    {
        /// <summary>
        /// 解析表达式，并计算其结果
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Variable CalculateExpression(string expression)
        {
            Variable result =  ExpressionParser.ComputeExpression(expression);
            TableManager.ReleaseUnBuiltInVariable();
            return result;
        }

        #region 初始化引擎，内置操作符与函数
        static ExpEngine()
        {
            InitialOperator();
            InitialFunction();
            InitialSymbol();
            InitialVariable();
        }
        #endregion

        #region 注册全局变量
        private static void InitialVariable()
        {
            TableManager.RegistVariable(new Variable() { Name = "TRUE", Value = true, ValueType = DataType.BOOL, BBuiltIn = true});
            TableManager.RegistVariable(new Variable() { Name = "FALSE", Value = false, ValueType = DataType.BOOL, BBuiltIn = true});
        }
        #endregion

        #region 注册关键字
        private static void InitialSymbol()
        {
            TableManager.RegistSyntaxKeys(new Symbol() { Identifier = "(", Escape = "(" });
            TableManager.RegistSyntaxKeys(new Symbol() { Identifier = ")", Escape = ")" });
            TableManager.RegistSyntaxKeys(new Symbol() { Identifier = ",", Escape = "," });
            TableManager.RegistSyntaxKeys(new Symbol() { Identifier = "，", Escape = "，" });
        }
        #endregion

        #region 初始化内置操作符：+，-，*，/,Mod,%,^,等等
        private static void InitialOperator()
        {
            /// 加法操作符
            TableManager.RegistOperator(new Operator(new PlusOperatorHandler()) { Identifier = "+", Dimension = 2, Escape = "+", Priority = 2 });

            ///减法操作符
            TableManager.RegistOperator(new Operator(new SubtractOperatorHandler()) { Identifier = "-", Dimension = 2, Escape = "-", Priority = 2 });

            ///取余数操作符
            TableManager.RegistOperator(new Operator(new ModOperatorHandler()) { Identifier = "%", Dimension = 2, Escape = "%", Priority = 3 });

            ///乘法操作符
            TableManager.RegistOperator(new Operator(new MultiplyOperatorHandler()) { Identifier = "*", Dimension = 2, Escape = "*", Priority = 5 });

            ///除法操作符
            TableManager.RegistOperator(new Operator(new DivisionOperatorHandler()) { Identifier = "/", Dimension = 2, Escape = "/", Priority = 5 });

            ///取负号操作符
            TableManager.RegistOperator(new Operator(new NegativeOperatorHandler()) { Identifier = "_", Dimension = 1, Escape = "-", Priority = 6 });

            ///取次方操作符
            TableManager.RegistOperator(new Operator(new PowerOperatorHandler()) { Identifier = "^", Dimension = 1, Escape = "^", Priority = 7 });

            ///大于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = ">", Dimension = 2, Escape = ">", Priority = 1 });

            ///小于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = "<", Dimension = 2, Escape = "<", Priority = 1 });

            ///大于或等于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = ">=", Dimension = 2, Escape = ">=", Priority = 1 });

            ///小于或等于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = "<=", Dimension = 2, Escape = "<=", Priority = 1 });

            ///小于或等于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = "==", Dimension = 2, Escape = "==", Priority = 1 });

            ///小于或等于操作符
            TableManager.RegistOperator(new Operator(new LogicOperatorHandler()) { Identifier = "!=", Dimension = 2, Escape = "!=", Priority = 1 });
        }
        #endregion

        #region 初始化内置函数:Tan(), ATan()等
        private static void InitialFunction()
        {
            TableManager.RegistFunction(new Function() { Identifier = "TAN", BStatic = false, Clazz = "ExpressionEngine.BaseMath", Dimension = 1, Escape = "TAN", Priority = 10 });
            TableManager.RegistFunction(new Function() { Identifier = "IF_ELSE", BStatic = false, Clazz = "ExpressionEngine.BaseMath", Dimension = 3, Escape = "IF", Priority = 10 });
            TableManager.RegistFunction(new Function() { Identifier = "OR", BStatic = false, Clazz = "ExpressionEngine.BaseMath", Dimension = 2, Escape = "OR", Priority = 0 });
            TableManager.RegistFunction(new Function() { Identifier = "AND", BStatic = false, Clazz = "ExpressionEngine.BaseMath", Dimension = 2, Escape = "AND" , Priority = 0});
            TableManager.RegistFunction(new Function() { Identifier = "NOT", BStatic = false, Clazz = "ExpressionEngine.BaseMath", Dimension = 1, Escape = "NOT" , Priority =0 });
        }
        #endregion
    }
}
